package com.zhanglifan.action;

import com.opensymphony.xwork2.ModelDriven;
import com.zhanglifan.domain.ContractProduct;
import com.zhanglifan.domain.Factory;
import com.zhanglifan.service.ContractProductService;
import com.zhanglifan.service.ContractService;
import com.zhanglifan.service.FactoryService;
import com.zhanglifan.util.Page;

import java.util.List;

/**
 * Information
 * Author: ZhangLifan
 * Time: 2017/10/6 20:08
 * Description: ContractProductAction
 */
public class ContractProductAction extends BaseAction implements ModelDriven<ContractProduct>{
    private ContractProduct contractProduct = new ContractProduct();
    private ContractService contractService;
    private ContractProductService contractProductService;
    private FactoryService factoryService;

    private Page<ContractProduct> page = new Page<>();

    public ContractProductAction() {
        System.out.println("+++++++");
    }

    public String tocreate(){
        List<Factory> factoryList = factoryService.find("from Factory where ctype='货物'", Factory.class, null);
        super.put("factoryList",factoryList);
        contractProductService.findPage("from ContractProduct where contract.id=?",page,ContractProduct.class,new String[]{contractProduct.getContract().getId()});
        page.setUrl("contractProductAction_tocreate");
        super.push(page);
        return "toCreateSuccess";
    }

    public String insert(){
        contractProductService.saveOrUpdate(contractProduct);
        return "insertSuccess";
    }

    public String toupdate(){
        List<Factory> factoryList = factoryService.find("from Factory where ctype='货物'", Factory.class, null);
        super.put("factoryList",factoryList);
        ContractProduct contractProduct = contractProductService.get(ContractProduct.class, this.contractProduct.getId());
        super.push(contractProduct);
        return "toUpdateSuccess";
    }

    public String update(){
        ContractProduct cp = contractProductService.get(ContractProduct.class, this.contractProduct.getId());
        cp.setFactory(factoryService.get(Factory.class,contractProduct.getFactory().getId()));
        cp.setFactoryName(contractProduct.getFactoryName());
        cp.setProductNo(contractProduct.getProductNo());
        cp.setProductImage(contractProduct.getProductImage());
        cp.setCnumber(contractProduct.getCnumber());
        cp.setPackingUnit(contractProduct.getPackingUnit());
        cp.setLoadingRate(contractProduct.getLoadingRate());
        cp.setBoxNum(contractProduct.getBoxNum());
        cp.setPrice(contractProduct.getPrice());
        cp.setOrderNo(contractProduct.getOrderNo());
        cp.setProductDesc(contractProduct.getProductDesc());
        cp.setProductRequest(contractProduct.getProductRequest());
        contractProductService.saveOrUpdate(cp);
        return "updateSuccess";
    }

    public String delete(){
        contractProductService.deleteById(ContractProduct.class,contractProduct.getId());
        return tocreate();
    }

    @Override
    public ContractProduct getModel() {
        return contractProduct;
    }

    public void setContractProductService(ContractProductService contractProductService) {
        this.contractProductService = contractProductService;
    }

    public void setContractService(ContractService contractService) {
        this.contractService = contractService;
    }

    public void setFactoryService(FactoryService factoryService) {
        this.factoryService = factoryService;
    }

    public Page<ContractProduct> getPage() {
        return page;
    }
}
